package com.aigou.admin.controller;

import com.aigou.admin.entity.Brand;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.ResponseEntity;
import com.aigou.admin.service.BrandService;
import com.aigou.admin.service.impl.BrandServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand")
public class BrandController extends BaseServlet {

    private BrandService brandService = new BrandServiceImpl();

    public void page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        //查询商品
        PageEntity<Brand> brands = brandService.findBrandPage(Integer.valueOf(current),Integer.valueOf(pageSize));
        ResponseEntity.ok(resp,brands);
    }

    //将方法名称作为参数传递 m=save
    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获得品牌id
        String brandId = req.getParameter("id");
        String brandName = req.getParameter("brand_name");
        String brandType = req.getParameter("brand_type");
        String brandImg = req.getParameter("brand_img");
        Brand brand = new Brand(brandId,brandName,brandType,brandImg);
        //查询品牌
        boolean rs = brandService.save(brand);
        ResponseEntity.ok(resp,rs);

    }

    public void remove(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String brandId = req.getParameter("id");
        boolean rs = brandService.remove(brandId);
        ResponseEntity.ok(resp,rs);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获得品牌id
        String brandId = req.getParameter("id");
        String brandName = req.getParameter("brand_name");
        String brandType = req.getParameter("brand_type");
        String brandImg = req.getParameter("brand_img");
        Brand brand = new Brand(brandId,brandName,brandType,brandImg);
        boolean rs = brandService.update(brand);
        ResponseEntity.ok(resp,rs);
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //查询商品
        List<Brand> brands = brandService.findAll();
        ResponseEntity.ok(resp,brands);
    }

    public void findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String brandId = req.getParameter("id");
        Brand brand = brandService.findById(brandId);
        ResponseEntity.ok(resp,brand);
    }

}
